package com.bdqn.Test;

import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SysUserTest {
    private Logger logger = Logger.getLogger(SysUserTest.class);

    /**
     * Rigorous Test :-)
     */
    @Test
    public void testGetUserList() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        SysUserService userService = (SysUserService) ctx.getBean("sysUserService");
        List<SysUser> userList = new ArrayList<SysUser>();
        SysUser sysUser = new SysUser();
        sysUser.setRealName("赵");
        sysUser.setRoleId(2);
        userList = userService.getList(sysUser);

        for (SysUser userResult : userList) {
            logger.debug("testGetUserList account: " + userResult.getAccount() + " and realName: " + userResult.getRealName() + " and roleId: " + userResult.getRoleId() + " and roleName: " + userResult.getRoleIdName() + " and address: " + userResult.getAddress());
        }
    }

    /**
     * 测试 保存用户
     */
    @Test
    public void testAddUser() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        SysUserService userService = (SysUserService) ctx.getBean("sysUserService");
        SysUser user = new SysUser();
        user.setAccount("test001");
        user.setRealName("测试用户001");
        user.setPassword("1234567");
        Date birthday = null;
        try {
            birthday = new SimpleDateFormat("yyyy-MM-dd").parse("2021-05-17");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setBirthday(birthday);
        user.setAddress("地址测试");
        user.setSex(1);
        user.setPhone("13688783697");
        user.setRoleId(1);
        user.setCreatedUserId(1);
        user.setCreatedTime(new Date());
        boolean isok = userService.add(user);
        logger.debug("testAdd result: " + isok);
    }
}